<style scoped>
/* 背景渐变和动画 */
.sheet-bg {
  background:
    linear-gradient(
      135deg,
      rgba(74, 144, 226, 0.8) 0%,
      rgba(80, 200, 120, 0.6) 25%,
      rgba(255, 193, 7, 0.7) 50%,
      rgba(255, 87, 34, 0.8) 75%,
      rgba(156, 39, 176, 0.9) 100%
    ),
    url('../assets/img/home-bg/bg-3.jpg');
  background-size: cover, cover;
  background-position: center, center;
  background-blend-mode: overlay, normal;
  position: relative;
  overflow: hidden;
}

/* 主容器样式 */
.main-container {
  position: relative;
  z-index: 2;
  backdrop-filter: blur(20px);
}

/* 卡片样式优化 */
.v-card {
  border-radius: 12px;
  backdrop-filter: blur(20px);
  background: rgba(255, 255, 255, 0.95);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

/* 标题样式 */
.v-card-title h1 {
  background: linear-gradient(135deg, #4caf50 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  font-weight: 700;
  letter-spacing: -0.5px;
  margin-bottom: 8px;
}

/* 标签页样式 */
.v-tabs {
  border-bottom: 2px solid rgba(102, 126, 234, 0.1);
  margin-bottom: 24px;
  position: relative;
}

.v-tabs::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, rgba(102, 126, 234, 0.3) 0%, rgba(118, 75, 162, 0.3) 100%);
  border-radius: 1px;
}

.v-tab {
  font-weight: 600;
  text-transform: none;
  letter-spacing: 0.5px;
  font-size: 16px;
  padding: 16px 24px;
  transition: all 0.3s ease;
  position: relative;
}

.v-tab:hover {
  color: #4caf50;
  transform: translateY(-2px);
}

.v-tab--selected {
  color: #4caf50;
  font-weight: 700;
}

/* 按钮样式 */
.v-btn {
  text-transform: none;
  letter-spacing: 0.5px;
  font-weight: 600;
  font-size: 16px;
  padding: 16px 32px;
  border-radius: 16px;
  position: relative;
  overflow: hidden;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.v-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  transition: left 0.5s ease;
}

.v-btn:hover::before {
  left: 100%;
}

.v-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 16px 32px rgba(0, 0, 0, 0.15);
}

.v-btn:active {
  transform: translateY(-1px);
}

/* 切换按钮样式 */
.toggle-btn {
  background: linear-gradient(135deg, #4caf50 0%, #66bb6a 100%);
  color: white;
  border-radius: 12px;
  padding: 8px 16px;
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  min-width: 120px;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.toggle-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 16px rgba(102, 126, 234, 0.3);
}

/* 切换提示区域 */
.toggle-section {
  margin-top: 24px;
  padding: 16px;
  background: rgba(102, 126, 234, 0.05);
  border-radius: 16px;
  border: 1px solid rgba(102, 126, 234, 0.1);
  backdrop-filter: blur(10px);
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.toggle-content {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 28px;
  transition: all 0.3s ease;
  position: relative;
}

.toggle-content span {
  transition: opacity 0.2s ease;
}

.toggle-btn {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.text-medium-emphasis {
  opacity: 0.8;
  font-weight: 500;
  color: rgba(0, 0, 0, 0.7);
}

/* 加载动画 */
/* .v-btn--loading {
  position: relative;
}

.v-btn--loading .v-btn__content {
  opacity: 0;
}

.v-btn--loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 20px;
  height: 20px;
  margin: -10px 0 0 -10px;
  border: 2px solid transparent;
  border-top: 2px solid currentColor;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
} */

/* 响应式优化 */
@media (max-width: 960px) {
  .card-container {
    flex: 0 0 63%;
  }
}
@media (max-width: 600px) {
  .card-container {
    flex: 0 0 90%;
  }
  .v-card {
    margin: 16px;
    padding: 20px !important;
  }

  .v-btn {
    padding: 14px 24px;
    font-size: 15px;
  }

  .v-tab {
    padding: 12px 16px;
    font-size: 15px;
  }
}

@media (max-width: 400px) {
  .v-card {
    margin: 8px;
    padding: 16px !important;
  }

  .sheet-bg {
    padding: 8px;
  }
}

/* 浮动装饰元素 */
.floating-elements {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

.floating-circle {
  position: absolute;
  border-radius: 50%;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.circle-1 {
  width: 80px;
  height: 80px;
  top: 20%;
  left: 10%;
  animation-delay: 0s;
}

.circle-2 {
  width: 120px;
  height: 120px;
  top: 60%;
  right: 15%;
  animation-delay: 2s;
}

.circle-3 {
  width: 60px;
  height: 60px;
  top: 80%;
  left: 20%;
  animation-delay: 4s;
}

.circle-4 {
  width: 100px;
  height: 100px;
  top: 30%;
  right: 30%;
  animation-delay: 1s;
}

@keyframes float {
  0%,
  100% {
    transform: translateY(0px) rotate(0deg);
    opacity: 0.7;
  }
  25% {
    transform: translateY(-20px) rotate(90deg);
    opacity: 1;
  }
  50% {
    transform: translateY(-10px) rotate(180deg);
    opacity: 0.8;
  }
  75% {
    transform: translateY(-30px) rotate(270deg);
    opacity: 0.9;
  }
}

/* 标签页图标样式 */
.tab-item {
  gap: 8px;
}

.tab-item .v-icon {
  transition: all 0.3s ease;
}

.tab-item:hover .v-icon {
  transform: scale(1.1);
}

/* 头像样式 */
/* .v-avatar {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  box-shadow: 0 8px 16px rgba(102, 126, 234, 0.3);
} */

/* 分割线样式 */
.v-divider {
  border-color: rgba(102, 126, 234, 0.2);
}

/* 深色模式支持 */
@media (prefers-color-scheme: dark) {
  .v-card {
    background: rgba(30, 30, 30, 0.95);
    border-color: rgba(255, 255, 255, 0.1);
  }

  .v-text-field :deep(.v-field) {
    background: rgba(40, 40, 40, 0.8);
    border-color: rgba(102, 126, 234, 0.2);
  }

  .text-medium-emphasis {
    color: rgba(255, 255, 255, 0.7);
  }

  .floating-circle {
    background: linear-gradient(
      135deg,
      rgba(255, 255, 255, 0.05) 0%,
      rgba(255, 255, 255, 0.02) 100%
    );
    border-color: rgba(255, 255, 255, 0.05);
  }
}
</style>

<template>
  <v-sheet
    class="d-flex flex-column align-center justify-center overflow-x-auto sheet-bg overflow-y-hidden main-container"
    style="height: 100vh"
  >
    <!-- 装饰性背景元素 -->
    <!-- <div class="floating-elements">
      <div class="floating-circle circle-1"></div>
      <div class="floating-circle circle-2"></div>
      <div class="floating-circle circle-3"></div>
      <div class="floating-circle circle-4"></div>
    </div> -->

    <v-row class="justify-center align-center" style="width: 100%; min-width: 300px; height: 100vh">
      <v-col class="card-container" cols="11" xl="3" lg="4" md="5" sm="11" style="max-width: 100%">
        <v-card class="mx-auto px-8 py-10" max-width="100%" elevation="5">
          <!-- 卡片头部 -->
          <v-card-title class="text-center pb-2">
            <div class="d-flex align-center justify-center mb-2">
              <v-avatar size="48" class="mr-3">
                <v-img :src="logoSrc"></v-img>
              </v-avatar>
              <h1 class="text-h4">{{ settings.title }}</h1>
            </div>
            <p class="text-body-2 text-medium-emphasis">
              {{
                currentMode === 'login' ? '欢迎回来，请登录您的账户' : '创建新账户，留下您的足迹'
              }}
            </p>
          </v-card-title>

          <!-- 模式切换按钮 -->
          <v-tabs
            v-model="currentMode"
            class="mb-6"
            :color="currentMode === 'login' ? 'light-blue-accent-3' : 'success'"
            align="center"
          >
            <v-tab value="login" class="tab-item">
              <v-icon left>mdi-login</v-icon>
              登录
            </v-tab>
            <v-tab value="register" class="tab-item">
              <v-icon left>mdi-account-plus</v-icon>
              注册
            </v-tab>
          </v-tabs>

          <v-form v-model="form" @submit.prevent="onSubmit">
            <!-- 注册模式下的用户名字段 -->
            <v-text-field
              v-if="currentMode === 'register'"
              v-model="username"
              :readonly="loading"
              :rules="[required, username_rule]"
              append-inner-icon="mdi-account"
              class="mb-3 neu-input"
              placeholder="请输入您的用户名"
              clearable
              variant="outlined"
            ></v-text-field>

            <v-text-field
              v-model="email"
              :readonly="loading"
              :rules="emailRules"
              append-inner-icon="mdi-email"
              class="mb-3 neu-input"
              placeholder="请输入您的邮箱"
              clearable
              variant="outlined"
            ></v-text-field>

            <v-text-field
              v-model="password"
              :readonly="loading"
              type="password"
              class="mb-3 neu-input"
              append-inner-icon="mdi-lock"
              :rules="passwordRules"
              placeholder="请输入您的密码"
              clearable
              variant="outlined"
            ></v-text-field>

            <!-- 注册模式下的确认密码字段 -->
            <v-text-field
              v-if="currentMode === 'register'"
              v-model="confirmPassword"
              :readonly="loading"
              type="password"
              class="mb-4 neu-input"
              append-inner-icon="mdi-lock-check"
              :rules="confirmPasswordRules"
              placeholder="请再次输入密码"
              clearable
              variant="outlined"
            ></v-text-field>

            <!-- 提交按钮 -->
            <v-btn
              :disabled="!form"
              :loading="loading"
              :color="currentMode === 'login' ? 'light-blue-accent-3' : 'success'"
              size="x-large"
              type="submit"
              variant="elevated"
              block
              class="mb-4"
            >
              <v-icon left>{{ currentMode === 'login' ? 'mdi-login' : 'mdi-account-plus' }}</v-icon>
              {{ currentMode === 'login' ? '立即登录' : '立即注册' }}
            </v-btn>

            <!-- 切换模式提示 -->
            <div class="toggle-section text-center">
              <div class="toggle-content">
                <span class="text-body-2 text-medium-emphasis">
                  {{ currentMode === 'login' ? '还没有账号？' : '已有账号？' }}
                </span>
                <v-btn
                  variant="text"
                  color="teal-accent-2"
                  size="small"
                  class="toggle-btn ml-2"
                  @click="toggleMode"
                  :disabled="loading"
                >
                  <v-icon left size="small">{{
                    currentMode === 'login' ? 'mdi-account-plus' : 'mdi-login'
                  }}</v-icon>
                  {{ currentMode === 'login' ? '立即注册' : '立即登录' }}
                </v-btn>
              </div>
            </div>
          </v-form>
        </v-card>
      </v-col>
    </v-row>
  </v-sheet>
</template>
<script setup lang="ts">
import { useRouter } from 'vue-router'
import useSettings from '@/utils/settings'
import MessageBox from '@/components/Message'
import { userService } from '@/services/user.service'
import { ref, computed } from 'vue'
import logoSrc from '@/assets/img/logo.png'

const settings = useSettings()
const form = ref(false)
const username = ref('')
const email = ref('')
const password = ref('')
const confirmPassword = ref('')
const loading = ref(false)
const currentMode = ref('login') // 'login' 或 'register'

const router = useRouter()

// 切换模式
function toggleMode() {
  currentMode.value = currentMode.value === 'login' ? 'register' : 'login'
  // 清空表单
  username.value = ''
  email.value = ''
  password.value = ''
  confirmPassword.value = ''
  form.value = false
}

// 提交表单
function onSubmit() {
  if (!form.value) return
  loading.value = true

  const submitData: any = {
    email: email.value,
    password: password.value
  }

  // 注册模式需要添加用户名
  if (currentMode.value === 'register') {
    submitData.username = username.value
  }

  const apiCall =
    currentMode.value === 'login' ? userService.login(submitData) : userService.register(submitData)

  loading.value = false
  apiCall
    .then((res: any) => {
      if (res.code === 200) {
        const message = currentMode.value === 'login' ? '登录成功' : '注册成功'
        MessageBox({
          message,
          duration: 3000
        })
        // 登录成功后可以跳转到其他页面
        if (currentMode.value === 'login') {
          // router.push('/dashboard') // 根据实际路由调整
        }
      }
    })
    .catch((error: any) => {
      const message = currentMode.value === 'login' ? '登录失败' : '注册失败'
      MessageBox({
        message: error.message || message,
        duration: 3000
      })
    })
}

// 验证规则
function required(v: unknown) {
  return !!v || '必填项'
}

function username_rule(v: string) {
  if (currentMode.value === 'login') return true // 登录模式不需要用户名
  return !v || v.length <= 15 || '用户名长度不能大于15个字符'
}

function email_rule(v: string) {
  return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(v) || '请输入正确的邮箱地址'
}

function password_rule(v: string) {
  if (currentMode.value === 'login') {
    return !!v || '请输入密码' // 登录模式密码必填
  }
  return (
    /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d!@#$%^&*()_+]{8,16}$/.test(v) ||
    '密码长度为8-16位，包含数字、字母、特殊字符'
  )
}

function confirmPassword_rule(v: string) {
  if (currentMode.value === 'login') return true // 登录模式不需要确认密码
  return v === password.value || '两次输入的密码不一致'
}

// 计算属性 - 动态验证规则
const emailRules = computed(() => {
  return currentMode.value === 'login' ? [required, email_rule] : [email_rule]
})

const passwordRules = computed(() => {
  return currentMode.value === 'login' ? [required, password_rule] : [required, password_rule]
})

const confirmPasswordRules = computed(() => {
  return currentMode.value === 'register' ? [required, confirmPassword_rule] : []
})
</script>
